package com.hsyf.service.front;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.login.LoginContext;

import org.w3c.dom.ls.LSResourceResolver;

import com.alibaba.fastjson.JSON;
import com.hsyf.service.common.ResObj;
import com.hsyf.service.entity.userInfo;

import net.jdiy.core.App;
import net.jdiy.core.Args;
import net.jdiy.core.Dao;
import net.jdiy.core.JDiyAction;
import net.jdiy.core.Ls;
import net.jdiy.core.Rs;
import net.jdiy.util.Sec;

public class frontLogin extends JDiyAction{
	App app = App.get();
	Dao dao = app.getDao();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public void Login() throws IOException
	{
		userInfo uiInfo =new userInfo();
		String username, password, org;
		username =app.get("username");
		password =app.get("password");
		org = app.get("org");
		ResObj roObj = new ResObj();
		//Gson gson = new Gson();  
		try {
			app.getRequest().setCharacterEncoding("utf-8");
			app.getResponse().setCharacterEncoding("utf-8");
			Rs rs= dao.rs("SELECT t0,id,tid,t1,t2,t10 FROM jdiy_sys WHERE frame LIKE '%.z0000000s0.%' AND id<>'z0000000s0' and t11='0' and t0='"+username+"' and t10='"+org+"'");
			String roleidString="";
			Map roleopt = new HashMap();
			
				if(!Sec.MD5Encrypt(password, 32).equals(rs.get("t1")))
				{
					roObj.setRescode("1");
					roObj.setMsg("密码有误！");
					
					app.getResponse().getWriter().println(JSON.toJSONString(roObj));
				}
				uiInfo.setId(rs.get("id"));
				roObj.setUid(rs.get("id"));
				roleidString=rs.get("tid");
				uiInfo.setRoleid(roleidString);
				uiInfo.setTruename(rs.get("t2"));
				
				Rs roleRs = dao.rs("SELECT id,t0,t1,t15 FROM jdiy_sys where id='"+roleidString+"'");
				uiInfo.setRolename(roleRs.get("t0"));
				uiInfo.setOrgid(rs.get("t10"));
				Ls rolels= dao.ls("SELECT t0,id FROM front_menu WHERE id in("+roleRs.get("t15")+")", 0, 1);
				for(Rs opt:rolels.getItems())
				{
					roleopt.put(opt.get("id"), opt.get("t0"));
				}
				roObj.map.put("opts", roleopt);
				roObj.rescode="0";
				roObj.map.put("userinfo", uiInfo);
				
				//String str = gson.toJson(roObj);  
				//String str = JSON.toJSONString(roObj);
				app.getResponse().setHeader("Content-type", "application/json");  
				app.getResponse().setHeader("charset", "utf-8");
				app.getResponse().getWriter().println(JSON.toJSONString(roObj));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			roObj.setRescode("1");
			roObj.setMsg("操作失败");
			e.printStackTrace();
			app.getResponse().getWriter().println(JSON.toJSONString(roObj));
		} 

	}
	
	
	
}
